/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package helloscala.practise.chapter13.encodedstring

import impl._
/**
 * the boundary of the 'encodedstring' package/component, usually other parts/classes
 * are advised to be transparent to the user of this component
 */
trait EncodedString {
  protected[encodedstring] val string: String //visible only to the package
  val separator: EncodedString.Separator.Delimiter
  override def toString = string
  def toToken: List[String] = string.split(separator.toString) toList
}

/**
 * Companion object of trait 'EncodedString'
 */
object EncodedString {
  object Separator extends Enumeration {
    type Delimiter = Value
    val COMMA = Value(",")
    val DASH = Value("-")
  }

  def apply(string: String, separator: Separator.Delimiter): EncodedString = separator match {
    case Separator.COMMA => CSV(string)
    case Separator.DASH => TSV(string)
  }

  def unapply(es: EncodedString) = Some(Pair(es.string, es.separator))
}
